package cn.tedu.tmall.front.mall.service.impl;

import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.front.mall.dao.persist.cache.ICartCacheRepository;
import cn.tedu.tmall.front.mall.dao.persist.repository.IGoodRepository;
import cn.tedu.tmall.front.mall.pojo.po.CartPO;
import cn.tedu.tmall.front.mall.pojo.vo.CartListItemVO;
import cn.tedu.tmall.front.mall.pojo.vo.GoodsListItemVO;
import cn.tedu.tmall.front.mall.pojo.vo.GoodsStandardVO;
import cn.tedu.tmall.front.mall.service.ICartService;
import cn.tedu.tmall.front.mall.util.GoodsToCartListItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class CartServiceImpl implements ICartService {
    @Autowired
    private ICartCacheRepository cartCacheRepository;
    @Autowired
    private IGoodRepository goodRepository;
    @Override
    public void add(CurrentPrincipal principal, Long goodsId, Integer goodsNum) {
        //TODO 1.判断该商品是否存在
        //TODO 2.判断该商品是否下架
        GoodsStandardVO goodsStandardVO = goodRepository.selectById(goodsId);
        if (goodsStandardVO == null) {
            String message = "添加购物车失败，商品数据不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        if (goodsStandardVO.getIsPutOn() != 1) {
            String message = "添加购物车失败，该商品已下架";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        CartPO cartPO = new CartPO(goodsId,goodsNum);
        CartPO cacheCartPO = cartCacheRepository.getByUserIdAnyGoodsId(principal.getId(), goodsId);
        if (cacheCartPO != null) {
            cartPO.setGoodsNum(cacheCartPO.getGoodsNum()+goodsNum);
        }
        System.out.println(cartPO);
        cartCacheRepository.put(principal.getId(),cartPO);
    }

    @Override
    public void deleteByUserIdGoodsID(CurrentPrincipal principal, Long goodsId) {
        cartCacheRepository.delete(principal.getId(),goodsId);
    }

    @Override
    public Integer increase(CurrentPrincipal principal, Long goodsId) {
        Integer goodsNum = increase(principal.getId(), goodsId, 1);
        return goodsNum;
    }

    @Override
    public Integer increase(CurrentPrincipal principal, Long goodsId, Integer num) {
        Integer goodsNum = increase(principal.getId(), goodsId, num);
        return goodsNum;
    }

    @Override
    public Integer reduce(CurrentPrincipal principal, Long goodsId) {
        Integer goodsNum = reduce(principal.getId(), goodsId, 1);
        return goodsNum;
    }

    @Override
    public Integer reduce(CurrentPrincipal principal, Long goodsId, Integer num) {
        Integer goodsNum = reduce(principal.getId(), goodsId, num);
        return goodsNum;
    }

    @Override
    public List<CartListItemVO> list(CurrentPrincipal principal) {
        List<CartListItemVO> list = new ArrayList<>();
        List<CartPO> cartPOList = cartCacheRepository.getListByUserId(principal.getId());
        for (CartPO cartPO : cartPOList) {
            GoodsStandardVO goodsStandardVO = goodRepository.selectById(cartPO.getGoodsId());
            CartListItemVO cartListItemVO = GoodsToCartListItemVO.convert(cartPO, goodsStandardVO);
            list.add(cartListItemVO);
        }
        return list;
    }

    private Integer increase(Long userId,Long goodsId,Integer num){
        CartPO cartPO = cartCacheRepository.getByUserIdAnyGoodsId(userId, goodsId);
        Integer goodsNum = cartPO.getGoodsNum()+num;
        CartPO cartPO1 = new CartPO(goodsId,goodsNum);
        cartCacheRepository.put(userId,cartPO1);
        return goodsNum;
    }

    private Integer reduce(Long userId,Long goodsId,Integer num){
        CartPO cartPO = cartCacheRepository.getByUserIdAnyGoodsId(userId, goodsId);
        Integer goodsNum = cartPO.getGoodsNum()-num;
        if(goodsNum<=0){
            cartCacheRepository.delete(userId, goodsId);
            return 0;
        }
        CartPO cartPO1 = new CartPO(goodsId,goodsNum);
        cartCacheRepository.put(userId,cartPO1);
        return goodsNum;
    }
}
